public class Main {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param board string字符串一维数组 
     * @param word string字符串 
     * @return bool布尔型
     */
    public static boolean exist(String[] board, String word) {
        // write code here
        char[][] arr = new char[board[0].length() + 1][board.length + 1];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length(); j++) {
                arr[i][j] = board[i].charAt(j);
            }
        }
        char[] ret = word.toCharArray();
        String str = search(arr,word.charAt(0));
        if(str.charAt(0) == 'f'){
            return false;
        }
        int prex =(int) str.charAt(1);
        int prey =(int) search(arr,word.charAt(0)).charAt(2);
        for(int i = 1;i < ret.length;i++){
            char cur = ret[i];
            String string = search(arr,cur);
            if(string.charAt(0) == 'f'){
                return false;
            }else{
                int curx =(int) string.charAt(1);
                int cury = (int)string.charAt(2);
                if((curx == prex + 1 && cury == prey + 1) || (curx == prex + 1 && cury == prey - 1)
                    ||(curx == prex -1 && cury == prey + 1) || ((curx == prex -1 && cury == prey - 1))){
                    return false;
                }
                prex = curx;
                prey = cury;
            }
        }
        return true;
    }
    private static String search(char[][] board, char w){
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if(board[i][j] == w){
                    str.append('t');
                    str.append(i);
                    str.append(j);
                    return str.toString();
                }
            }
        }
        str.append('f');
        return str.toString();
    }

    public static void main(String[] args) {
       String[] borad = {"XYZE","SFZS","XDEE"};
       String word = "XYZY";
       boolean ret = exist(borad,word);
        System.out.println(ret);
    }
}